<template>
  <div class="dt-main-tabs-content">
    <div class="dt-content-main">
      <div class="dt-content-head">
        <div class="dt-content-head-tab">
          <span class="dt-head-title">待办任务</span>
          <div class="dt-head-tabs">
            <el-tabs v-model="activeName" @tab-click="handleClick">
              <el-tab-pane label="我负责的" name="0"></el-tab-pane>
              <el-tab-pane label="我创建的" name="1"></el-tab-pane>
            </el-tabs>
          </div>
        </div>
        <wtComListSearch
          :sortType="sortType"
          :activeName="activeName"
          :showComplete="showComplete"
          @changeModel="changeModel"
          @changeShow="changeShow"
          @searchProject="searchProject"
        />
      </div>
      <div class="dt-content-page">
        <!-- <PublicEmpty
          v-if="
            activeName == '0' &&
            taskList1.length == 0 &&
            taskList3.length == 0 &&
            taskList4.length == 0 &&
            taskList5.length == 0 &&
            taskList6.length == 0
          "
          :ishowImage="true"
        /> -->
        <PublicLoadingTxt v-if="loading" />
        <WtComListCard
          v-else-if="sortType != '2'"
          @updataList="updataList"
          :taskList1="taskList1"
          :taskList2="taskList2"
          :taskList3="taskList3"
          :taskList4="taskList4"
          :taskList5="taskList5"
          :taskList6="taskList6"
          :taskList7="taskList7"
          :taskList8="taskList8"
          :taskList9="taskList9"
          :sortType="sortType"
          :activeName="activeName"
          @scrollTask="scrollTask"
        />

        <WtComListTable v-if="sortType == '2'" ref="ListTable" :taskList0="taskList0" @TurnPage="TurnPage"></WtComListTable>
        <!-- <wtComListWaitInfo />
        <WtComListCard /> -->
        <!-- <WtComListTable></WtComListTable> -->
      </div>
    </div>
  </div>
</template>
<script>
export default {
  data() {
    return {
      loading: false,
      Uid: this.$tools.getDecrypt(this.$store.state.uid), //用户id,
      activeName: this.$tools.getQuery(this).elTab || '0',
      page: 1,
      taskList0: {},
      taskList1: {},
      taskList2: {},
      taskList3: {},
      taskList4: {},
      taskList5: {},
      taskList6: {},
      taskList7: {},
      taskList8: {},
      taskList9: {},
      sortType: this.$tools.getQuery(this).sortType || '0',
      showComplete: this.$tools.getQuery(this).showComplete || 'true',
      keywords: '',
      projectId: ''
    }
  },
  computed: {
    // loading() {
    //   if (this.loadingArr.length === 0) {
    //     return false
    //   }
    //   // 使用数组的every方法来检查所有元素是否为true
    //   return this.loadingArr.every((value) => value === true)
    // }
  },
  methods: {
    handleClick(tab, event) {
      this.page = 1
      // this.$refs.ListTable.setPage(1)
      this.resetPage()
      // this.InitTaskList(this.activeName, this.keywords, this.page, this.projectId, JSON.parse(this.showComplete))
      var data = { elTab: tab.index, sortType: this.sortType, showComplete: this.showComplete }
      this.loading = true
      this.$router.push('/main/#/wt?tab=list&' + this.$qs.stringify(data))
    },
    resetPage() {
      if (this.$refs.ListTable) {
        this.$refs.ListTable.setPage(1)
      }
    },
    changeModel(value) {
      this.sortType = value
      // this.InitTaskList(this.activeName, this.keywords, this.page, this.projectId, JSON.parse(this.showComplete))
    },
    changeShow(val) {
      this.showComplete = String(val)
      this.InitTaskList(this.activeName, this.keywords, this.page, this.projectId, JSON.parse(this.showComplete))
    },
    searchProject(keywords, projectIds) {
      this.keywords = keywords
      this.projectId = projectIds
      this.InitTaskList(this.activeName, keywords, this.page, projectIds, JSON.parse(this.showComplete))
    },
    getTaskList(scope = '0', keywords = '', page = '1', projectId = '', selectType = '0', showComplete = true) {
      var data = [
          {
            scope: scope,
            userId: this.Uid,
            keywords: keywords,
            page: page,
            pageSize: '20',
            projectId: projectId,
            selectType: selectType,
            showComplete: showComplete
          }
        ],
        domain = [this.$store.state.DOMAIN],
        path = ['/project/task/todoTaskList'],
        dataAxios = this.$tools.getDataAxios(this, domain, path, data)
      return new Promise((resolve, reject) => {
        Promise.all(dataAxios)
          .then((response) => {
            this.$tools.callbackAxios(this, response, (response) => {
              var data = response[0].data
              if (data.code === 200) {
                switch (selectType) {
                  case '0':
                    this.taskList0 = data.data
                    break
                  case '1':
                    this.taskList1 = data.data
                    break // 防止代码自动落入下一个 case
                  case '2':
                    this.taskList2 = data.data
                    break // 防止代码自动落入下一个 case
                  case '3':
                    this.taskList3 = data.data
                    break // 防止代码自动落入下一个 case
                  case '4':
                    this.taskList4 = data.data
                    break // 防止代码自动落入下一个 case
                  case '5':
                    this.taskList5 = data.data
                    break // 防止代码自动落入下一个 case
                  case '6':
                    this.taskList6 = data.data
                    break // 防止代码自动落入下一个 case
                  case '7':
                    this.taskList7 = data.data
                    break // 防止代码自动落入下一个 case
                  case '8':
                    this.taskList8 = data.data
                    break // 防止代码自动落入下一个 case
                  case '9':
                    this.taskList9 = data.data
                    break // 防止代码自动落入下一个 case
                  default:
                  // 如果以上 case 都不匹配时执行的代码
                }
                resolve({ status: true }) // 当成功时，解决Promise并返回状态和数据
              } else {
                resolve({ status: true })
              }
            })
          })
          .catch((error) => {
            reject({ status: false })
          })
        // Promise.all(dataAxios)
        //   .then((response) => {
        //     // ...（其他代码不变）
        //     if (data.code === 200) {
        //       // ...（其他代码不变）
        //       resolve({ status: true, data: data.data }) // 当成功时，解决Promise并返回状态和数据
        //     } else {
        //       resolve({ status: false, data: null }) // 当失败时，也解决Promise并返回状态和数据
        //     }
        //   })
        //   .catch((error) => {
        //     reject({ status: false, error: error }) // 如果有错误，拒绝Promise并返回状态和错误
        //   })
      })
    },
    InitTaskList(scope = '0', keywords = '', page = 1, projectId = '', showComplete = true) {
      var promises = []
      if (this.activeName == '0') {
        if (this.sortType == '0') {
          promises.push(this.getTaskList(scope, keywords, page, projectId, '1', showComplete))
          promises.push(this.getTaskList(scope, keywords, page, projectId, '3', showComplete))
          promises.push(this.getTaskList(scope, keywords, page, projectId, '4', showComplete))
          promises.push(this.getTaskList(scope, keywords, page, projectId, '5', showComplete))
          promises.push(this.getTaskList(scope, keywords, page, projectId, '6', showComplete))
        } else if (this.sortType == '1') {
          promises.push(this.getTaskList(scope, keywords, page, projectId, '7', showComplete))
          promises.push(this.getTaskList(scope, keywords, page, projectId, '8', showComplete))
          promises.push(this.getTaskList(scope, keywords, page, projectId, '9', showComplete))
        } else {
          promises.push(this.getTaskList(scope, keywords, page, projectId, '0', showComplete))
        }
      } else {
        if (this.sortType == '0') {
          promises.push(this.getTaskList(scope, keywords, page, projectId, '1', showComplete))
          promises.push(this.getTaskList(scope, keywords, page, projectId, '2', showComplete))
          promises.push(this.getTaskList(scope, keywords, page, projectId, '3', showComplete))
          promises.push(this.getTaskList(scope, keywords, page, projectId, '4', showComplete))
          promises.push(this.getTaskList(scope, keywords, page, projectId, '5', showComplete))
          promises.push(this.getTaskList(scope, keywords, page, projectId, '6', showComplete))
        } else if (this.sortType == '1') {
          promises.push(this.getTaskList(scope, keywords, page, projectId, '7', showComplete))
          promises.push(this.getTaskList(scope, keywords, page, projectId, '8', showComplete))
          promises.push(this.getTaskList(scope, keywords, page, projectId, '9', showComplete))
        } else {
          promises.push(this.getTaskList(scope, keywords, page, projectId, '0', showComplete))
        }
      }
      // 使用Promise.all来处理所有的Promise
      Promise.all(promises)
        .then((results) => {
          this.loading = results.status
          // results.forEach((result) => {
          //   console.log('Status:', result.status) // 打印每个Promise的状态
          //   // 这里可以根据result.status和result.data来执行进一步的操作
          // })
        })
        .catch((error) => {
          this.loading = results.status
          console.error('An error occurred:', error)
        })
    },
    TurnPage(page) {
      this.page = page
      this.InitTaskList(this.activeName, this.keywords, this.page, this.projectId, JSON.parse(this.showComplete))
    },
    updataList() {
      this.InitTaskList(this.activeName, this.keywords, this.page, this.projectId, JSON.parse(this.showComplete))
    },
    scrollTask(page, selectType) {
      var data = [
          {
            scope: this.activeName,
            userId: this.Uid,
            keywords: this.keywords,
            page: page,
            pageSize: '20',
            projectId: this.projectId,
            selectType: selectType + '',
            showComplete: JSON.parse(this.showComplete)
          }
        ],
        domain = [this.$store.state.DOMAIN],
        path = ['/project/task/todoTaskList'],
        dataAxios = this.$tools.getDataAxios(this, domain, path, data)
      Promise.all(dataAxios)
        .then((response) => {
          this.$tools.callbackAxios(this, response, (response) => {
            var data = response[0].data
            if (data.code === 200) {
              switch (selectType + '') {
                case '0':
                  this.taskList0.items.push(...data.data.items)
                  break
                case '1':
                  this.taskList1.items.push(...data.data.items)
                  break // 防止代码自动落入下一个 case
                case '2':
                  this.taskList2.items.push(...data.data.items)
                  break // 防止代码自动落入下一个 case
                case '3':
                  this.taskList3.items.push(...data.data.items)
                  break // 防止代码自动落入下一个 case
                case '4':
                  this.taskList4.items.push(...data.data.items)
                  break // 防止代码自动落入下一个 case
                case '5':
                  this.taskList5.items.push(...data.data.items)
                  break // 防止代码自动落入下一个 case
                case '6':
                  this.taskList6.items.push(...data.data.items)
                  break // 防止代码自动落入下一个 case
                case '7':
                  this.taskList7.items.push(...data.data.items)
                  break // 防止代码自动落入下一个 case
                case '8':
                  this.taskList8.items.push(...data.data.items)
                  break // 防止代码自动落入下一个 case
                case '9':
                  this.taskList9.items.push(...data.data.items)
                  break // 防止代码自动落入下一个 case
                default:
                // 如果以上 case 都不匹配时执行的代码
              }
            }
          })
        })
        .catch((error) => {})
    }
  },
  watch: {
    $route(to, from) {
      // 当路由发生变化时，这个函数会被调用
      this.InitTaskList(this.activeName, this.keywords, this.page, this.projectId, JSON.parse(this.showComplete))
    }
  },
  mounted() {
    this.InitTaskList(this.activeName, this.keywords, this.page, this.projectId, JSON.parse(this.showComplete))
    this.$root.$on('updataList', (option) => {
      this.updataList()
    })
  }
}
</script>
<style scoped>
.dt-content-page {
  background-color: #f2f3f5;
  display: flex;
  flex-direction: column;
  overflow: auto;
  margin-top: 0;
  margin-bottom: 0;
  /* padding-bottom: 16px; */
}
.dt-head-tabs >>> .el-tabs__nav-scroll {
  transform: translateX(-32px);
}
</style>
